package com.hainatong.shiphelper.module.QSHE.fragment;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.module.QSHE.FileReaderActivity;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentListViewBinding;
import com.hainatong.shiphelper.module.QSHE.adapter.QSHEFileAdapter;
import com.hainatong.shiphelper.module.QSHE.entity.QSHEDetailBean;
import com.hainatong.shiphelper.module.QSHE.entity.QSHETypeBean;
import com.hainatong.shiphelper.module.QSHE.viewmodel.QSHEViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

public class SMISFragment extends MvvmLazyFragment<FragmentListViewBinding, QSHEViewModel>
{
    private static String SMIS_TAG = "SMISFragment";

    private QSHETypeBean qsheType;


    private QSHEFileAdapter adapter;

    public static SMISFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( SMIS_TAG , scanCode );
        SMISFragment smisFragment = new SMISFragment();
        smisFragment.setArguments( bundle );
        return smisFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            String qshrTypeStr = args.getString(SMIS_TAG , "");
            if( !StringUtils.isEmpty( qshrTypeStr ) )
            {
                qsheType = GsonUtil.json2Obj( qshrTypeStr , QSHETypeBean.class);
            }
        }
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        adapter = new QSHEFileAdapter();
        View header = getLayoutInflater().inflate(R.layout.driver_dp10 , (ViewGroup) viewDataBinding.recyclerview.getParent(),false);
        adapter.addHeaderView(header);
        RecyclerViewHelper.addLinerLayoutMgrNoDecoration( viewDataBinding.recyclerview );
        viewDataBinding.recyclerview.setAdapter( adapter );

        // 跳转pdf
        adapter.setOnItemClickListener( (adapter1, view1, position) -> {

          QSHEDetailBean  detailTemp = adapter.getItem( position );

          FileReaderActivity.start( getActivity() , detailTemp.getServerUrl() );
            //ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_DOCUMENT_VIEW, detailTemp.getServerUrl());
            //WebViewActivity.start(getActivity(), detailTemp.getServerUrl() );
        } );
    }

    @Override
    protected void lazyLoad()
    {
        if(ObjectUtils.isNotEmpty( qsheType ))
        {
            //查询 物资
            viewModel.queryQSHEFileDetailByDictId( qsheType.getUuid() );
        }

        viewModel.getQSHEFileListResult().observe( this , result -> {
            if( result.isSuccess() )
            {
                adapter.setList( result.getData() );
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );

    }
}
